package com.javalong.affairs.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.javalong.affairs.R;
import com.javalong.affairs.activity.DeptListActivity;
import com.javalong.affairs.activity.UserListActivity;
import com.javalong.affairs.adapter.DeptAdapter;
import com.javalong.affairs.constant.BundleKey;
import com.javalong.affairs.constant.DataType;
import com.javalong.affairs.constant.MapKey;
import com.javalong.affairs.data.AllData;
import com.javalong.affairs.manager.IntentManager;
import com.javalong.affairs.model.Dept;
import com.javalong.affairs.ui.SimplePullRefreshList;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

public class DeptListFragment extends BaseFragment implements
		OnItemClickListener {
	@ViewInject(R.id.lv_dept)
	private SimplePullRefreshList lv_dept;
	private DeptAdapter adapter;
	private List<Dept> deptList;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_dept, container, false);
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		lv_dept = (SimplePullRefreshList) view;
		deptList = AllData.GetInstance().appendListData(
				R.string.data_dept_list, new ArrayList<Dept>());
		adapter = new DeptAdapter(getActivity(), deptList, false);
		lv_dept.setAdapter(adapter);
		lv_dept.initData(getMapData());
		if (deptList.size() == 0) {
			lv_dept.refresh();
		}
		lv_dept.setOnItemClickListener(this);
	}

	private Map<String, Object> getMapData() {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put(MapKey.ACTION_NAME, "Section");
		param.put(MapKey.METHOD_NAME, "outList_section_all");
		param.put(MapKey.DATA_RESID, R.string.data_dept_list);
		param.put(MapKey.DATA_TYPE, DataType.LIST_DEPT);
		return param;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Bundle bundle = new Bundle();
		bundle.putInt(BundleKey.DEPTID, deptList.get(position).id);
		IntentManager
				.doActivity2(getActivity(), UserListActivity.class, bundle);
	}
}
